package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;


import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_VO2_DAY;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_VO2_MONTH;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_VO2_VALUE;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_VO2_YEAR;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;

import java.util.Calendar;

public class VO2MaxValue extends DialogFragment implements View.OnClickListener {
    private Context mContext;
    private ConfigCallBack mConfigCallback;
    private int startYear = 0;
    private int startMon = 0;
    private int startDay = 0;
    NumberPicker startYearPick;
    NumberPicker startMonPick;
    NumberPicker startDayPick;
    Calendar calendar = Calendar.getInstance();

    EditText ValueEd;
    public int v = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = inflater.getContext();
        View view = inflater.inflate(R.layout.config_vo2maxvalue, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        ValueEd = (EditText) view.findViewById(R.id.value);
        startYearPick = view.findViewById(R.id.year);
        startMonPick = view.findViewById(R.id.month);
        startDayPick = view.findViewById(R.id.day);

        int curYear = Calendar.getInstance().get(Calendar.YEAR);
        int curDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        startYearPick.setMinValue(curYear - 10);
        startYearPick.setMaxValue(curYear + 10);
        startYearPick.setValue(calendar.get(Calendar.YEAR));

        startMonPick.setMinValue(1);
        startMonPick.setMaxValue(12);
        startMonPick.setValue(calendar.get(Calendar.MONTH) + 1);

        startDayPick.setMinValue(1);
        startDayPick.setMaxValue(getDayTS(getYear(),getMonth()));
        startDayPick.setValue(curDay);

        startYear = curYear - 2000;
        startMon = calendar.get(Calendar.MONTH) + 1;
        startDay = curDay;

        startYearPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startDayPick.setMinValue(1);
                startDayPick.setMaxValue(getDayTS(getYear(),getMonth()));
                startYear = picker.getValue() - 2000;
            }
        });
        startMonPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startDayPick.setMinValue(1);
                startDayPick.setMaxValue(getDayTS(getYear(),getMonth()));
                startMon = picker.getValue();
            }
        });
        startDayPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startDay = picker.getValue();
                Log.i("TAG", "onValueChange: " + picker.getValue());
            }
        });

        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    private int getDayTS(int year, int month){
        int day = 30;
        boolean flag = false;
        switch (year % 4){
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag? 28:29;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    public int getYear(){
        return startYearPick.getValue();
    }

    public int getMonth(){
        return startMonPick.getValue();
    }

    public boolean saveConfig(){

        String HRvalue = ValueEd.getText().toString();
        if (TextUtils.isEmpty(HRvalue)) {
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 116", Toast.LENGTH_SHORT).show();
            return false;
        }
        v = Integer.valueOf(HRvalue);
        if (v > 116){
            Toast.makeText(getActivity(), "can not be empty & length can only be 0 - 116", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(),KEY_VO2_YEAR,startYear);
        SPHelper.putPreference(getActivity(),KEY_VO2_MONTH,startMon);
        SPHelper.putPreference(getActivity(),KEY_VO2_DAY,startDay);
        SPHelper.putPreference(getActivity(),KEY_VO2_VALUE,v);
        return true;
    }
    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }
    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };
    @Override
    public void onClick(View v) {
        dismissAllowingStateLoss();
        if (mConfigCallback != null){
            mConfigCallback.modeClick(v.getId());
            Log.i("TAG", "onClick: ============" + v.getId());
        }
    }


    public interface ConfigCallBack {

        void modeClick(int id);

        void onConfigOk();

        void onConfigCancel();
    }



}
